package com.rongshu.view.home.fragment.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.rongshu.R;
import com.rongshu.view.home.widget.FontSizeView;
import com.utils.SPUtils;
import com.utils.constants.SPKey;

public class FontSizeDialog extends Dialog {

    public FontSizeDialog(@NonNull Context context) {
        super(context);
    }

    public FontSizeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected FontSizeDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {

        private Context context = null;

        private int mMainLayout = R.layout.fragment_font_size_dialog_view;

        private FontSizeView fontSizeView;

        private FontSizeView.OnChangeCallbackListener listener = null;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setOnChangeCallbackListener(FontSizeView.OnChangeCallbackListener listener) {
            this.listener = listener;
            return this;
        }

        public FontSizeDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final FontSizeDialog dialog = new FontSizeDialog(context, R.style.CustomDialogStyle);
            View layout = inflater.inflate(mMainLayout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.setWindowAnimations(R.style.DefaultAnimation);

            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = (int) context.getResources().getDimension(R.dimen.dd_dimen_268px);
            window.setAttributes(lp);

            fontSizeView = layout.findViewById(R.id.fontSizeView);
            //类型
            int fontType = SPUtils.getPreferences(SPKey.FONTPATTERN, 1);
            fontSizeView.setDefaultPosition(fontType);
            fontSizeView.setChangeCallbackListener(listener);
            return dialog;
        }
    }
}
